﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Wormhole.UI.Application.Controls
{
    /// <summary>
    /// </summary>
    public class Card : Image
	{
		#region Declarations
		private bool isFirstClick = true;
		private bool isDoubleClick = false;
		private System.Windows.Forms.Timer doubleClickTimer = new System.Windows.Forms.Timer();
		private int timeBetweenClicks = 0;
		#endregion

		#region Properties
		/// <summary>
		/// The small card image.
		/// </summary>
		public BitmapImage SmallCardImage { get; set; }
		#endregion

		#region Constructor
		/// <author>Rodney McKay</author>
		/// <date>22 April 2008</date>
        /// <summary>
        /// Create a default instance of the class.
        /// </summary>
        public Card() : base()
        {
            this.Height = 121;
            this.Width = 131;
            this.Stretch = Stretch.None;
            this.VerticalAlignment = VerticalAlignment.Top;
            this.HorizontalAlignment = HorizontalAlignment.Left;
			this.SmallCardImage = new BitmapImage(new Uri("/Wormhole.UI.Application;component/images/sample02.png", UriKind.Relative));
			this.SmallCardImage.BaseUri = new Uri("pack://application:,,,/Wormhole.UI.Application;component/controls/score.xaml");

			// wire up the event for the click timer
			this.doubleClickTimer.Tick += new EventHandler(doubleClickTimer_Tick);
			this.DrawCard();
        }
        #endregion

		#region On Mouse Down
		/// <summary>
        /// Handle the mouse down event.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
			if (e.LeftButton == MouseButtonState.Pressed)
			{
				if (this.isFirstClick)
				{
					this.isFirstClick = false;
					this.doubleClickTimer.Start();
				}
				else
				{
					if (this.timeBetweenClicks < System.Windows.Forms.SystemInformation.DoubleClickTime)
						this.isDoubleClick = true;
				}
			}
        }
		#endregion

		#region Double Click Timer Tick
		/// <summary>
		/// This method is used to determine if a double or single click was executed.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void doubleClickTimer_Tick(object sender, EventArgs e)
		{
			this.timeBetweenClicks += 400;
			if (timeBetweenClicks > System.Windows.Forms.SystemInformation.DoubleClickTime)
			{
				this.doubleClickTimer.Stop();

				if (this.isDoubleClick)
					this.OnDoubleClick();
				else
					this.OnSingleClick();

				// reset the click members
				this.isFirstClick = true;
				this.isDoubleClick = false;
				this.timeBetweenClicks = 0;
			}
		}
		#endregion

		#region On Single Click
		/// <summary>
		/// Single click handler.
		/// </summary>
		private void OnSingleClick()
		{
			//CardDetails details = new CardDetails("/Wormhole.UI.Application;component/images/1c136.jpg");
			//details.Show();
		}
		#endregion

		#region On Double Click
		/// <summary>
		/// The Double click event handler.
		/// </summary>
		private void OnDoubleClick()
		{
			MessageBox.Show("Double Click");
		}
		#endregion

		#region Draw Card
		/// <summary>
		/// Draw the card and any borders or icons on it.
		/// </summary>
		protected void DrawCard()
		{
			// get the drawing context
			DrawingVisual drawingVisual = new DrawingVisual();
			DrawingContext drawingContext = drawingVisual.RenderOpen();

			// draw the card image
			drawingContext.DrawImage(this.SmallCardImage, new Rect(0, 0, this.SmallCardImage.Width, this.SmallCardImage.Height));

			// draw a rectangle
			Pen blackPen = new Pen(Brushes.Black, 6);
			Pen greenPen = new Pen(Brushes.LawnGreen, 3);

			drawingContext.DrawRoundedRectangle(Brushes.Transparent, blackPen, new Rect(1, 1, this.SmallCardImage.Width - 2, this.SmallCardImage.Height - 2), 6, 6);
			drawingContext.DrawRoundedRectangle(Brushes.Transparent, greenPen, new Rect(1, 1, this.SmallCardImage.Width - 2, this.SmallCardImage.Height - 2), 6, 6);

			// get the image source
			drawingContext.Close();
			RenderTargetBitmap targetBitmap = new RenderTargetBitmap(this.SmallCardImage.PixelWidth, this.SmallCardImage.PixelHeight, 96, 96, PixelFormats.Pbgra32);
			targetBitmap.Render(drawingVisual);

			// assign the image source
			this.Source = targetBitmap;
		}
		#endregion
	}
}
